package com.bbs.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bbs.dao.ModeratorDao;
import com.bbs.dao.Userdao;
import com.bbs.entity.Article;
import com.bbs.entity.Blocks;
import com.bbs.entity.Collection;
import com.bbs.entity.Message;
import com.bbs.entity.Reply;
import com.bbs.entity.User;
import com.bbs.service.BlocksService;
import com.bbs.service.UserService;
import com.bbs.util.ChangeUtil;
import com.bbs.util.Page;
import com.bbs.util.Render;

public class ApplicationServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("_method");
		
		if("".equals(method) || method == null){
			index(request,response);
		} else if(method.equals("register")){
			register(request,response);
		} else if(method.equals("login")){
			login(request,response);
		} else if(method.equals("create")){
			create(request,response);
		} else if("showarticle".equals(method)) {
			showarticle(request, response);
		} else if("manage".equals(method)){
			managelogin(request,response);
		} else if("managemain".equals(method)){
			managemain(request,response);
		} else if("showuser".equals(method)){
			showUser(request,response);
		} else if("setmoderator".equals(method)){
			chooseblocks(request,response);
		} else if("showblocks".equals(method)) {
			showblocks(request, response);
		} else if("showmain".equals(method)){
			showmain(request, response);
		} else if("choose".equals(method)){
			resetModerator(request,response);
		} else if("manageblocks".equals(method)){
			manageblocks(request,response);
		} else if("usercenter".equals(method)){
			usercenter(request,response);
		} else if("userchange".equals(method)){
			userchange(request,response);
		} else if("showsearch".equals(method)) {
			showsearch(request,response);
		} else {
			response.sendError(404);
		}
	}
	
	private void showsearch(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Render.rend("search.jsp", request, response);
	}

	@SuppressWarnings("unchecked")
	private void usercenter(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User u = (User) request.getSession().getAttribute("user");
		if(u == null){
			Render.rend("login", request, response);
		} else {
			if(request.getSession().getAttribute("alist") != null){
				int size = ((List<Article>)request.getSession().getAttribute("alist")).size();
				request.setAttribute("size", size);
			} else if(request.getSession().getAttribute("myreplylist") != null){
				int size = ((List<Reply>)request.getSession().getAttribute("myreplylist")).size();
				request.setAttribute("size", size);
			} else if(request.getSession().getAttribute("msglist") != null){
				int size = ((List<Message>)request.getSession().getAttribute("msglist")).size();
				request.setAttribute("size", size);
			} else if(request.getSession().getAttribute("collectionlist") != null){
				int size = ((List<Collection>)request.getSession().getAttribute("collectionlist")).size();
				request.setAttribute("size", size);
			}
			Render.rend("usercenter", request, response);
		}
	}

	private void userchange(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User u = (User) request.getSession().getAttribute("user");
		if(u == null){
			Render.rend("login", request, response);
		} else {
			Render.rend("userchange",request,response);
		}
	}

	private void manageblocks(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User u = (User) request.getSession().getAttribute("manager");
		if(u == null){
			Render.rend("admin/managelogin", request, response);
		} else {
			Render.rend("admin/manageblocks", request, response);
		}
	}

	/**
	 * 取消版主
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void resetModerator(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String uid = request.getParameter("id");
		User u = new Userdao().findModerator(Integer.parseInt(uid));
		if(u.getMlist().size() == 1){
			u.setRole(1);
			new ModeratorDao().delete(u.getId(),u.getMlist().get(0).getId());
			new UserService().update(u);
			response.sendRedirect("user.jspx?_method=showusers");
		} else {
			request.setAttribute("blocksuser", u);
			Render.rend("admin/showblocks", request, response);
		}
		
	}

	/**
	 * 板块显示
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void showblocks(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String id = request.getParameter("id");
		String p = request.getParameter("page");
		ChangeUtil cu = new ChangeUtil();
		int pid = cu.changeStringToInt(p);
		int bid = cu.changeStringToInt(id);
		boolean flag = false;
		if(pid <= 0 || bid == 0) {
			response.sendError(404);
		} else {
			
			Blocks blocks = (Blocks)session.getAttribute("blocks");
			Page page = (Page)session.getAttribute("page");
			if(blocks == null || page == null) {
				response.sendRedirect("blocks.jspx?_method=show&id="+bid+"&page="+pid);
				flag = true;
			}
			if(blocks != null) {
				if(!id.equals(String.valueOf(blocks.getId()))) {
					response.sendRedirect("blocks.jspx?_method=show&id="+bid+"&page="+pid);
					flag = true;
				}
			}
			if(page != null) {
				if(!p.equals(String.valueOf(page.getCurrentPage()))) {
					response.sendRedirect("blocks.jspx?_method=show&id="+bid+"&page="+pid);
					flag = true;
				} 
			}
			if(!flag) {
				Render.rend("blocks", request, response);
			}
		}
	}
	
	/**
	 * 选择板块
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void chooseblocks(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("manager");
		if(u == null){
			Render.rend("admin/managelogin", request, response);
		} else {
			session.setAttribute("uid", id);
			List<Blocks> blist = new BlocksService().findZBlocks();
			request.setAttribute("blist", blist);
			Render.rend("admin/showblocks", request, response);
		}
	}

	/**
	 * 后台显示主页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void showUser(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User u = (User) request.getSession().getAttribute("manager");
		if(u == null){
			Render.rend("admin/managelogin", request, response);
		} else {
			Render.rend("admin/managemain", request, response);
		}
	}

	/**
	 * 后台显示主页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void managemain(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		User u = (User) request.getSession().getAttribute("manager");
		if(u == null){
			Render.rend("admin/managelogin", request, response);
		} else {
			Render.rend("admin/managemain", request, response);
		}
	}

	/**
	 * 后台登陆
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void managelogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Render.rend("admin/managelogin", request, response);
	}

	/**
	 * 帖子跳转页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void showarticle(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String p = request.getParameter("page");
		ChangeUtil cu = new ChangeUtil();
		int aid;
		if(id == null) {
			aid = 0;
		} else {
			aid = cu.changeStringToInt(id);
		}
		int pid;
		if(p == null) {
			pid = 1;
		} else {
			pid = cu.changeStringToInt(p);
		}
		boolean flag = false;
		HttpSession session = request.getSession();
		Article article = (Article)session.getAttribute("article");
		Page page = (Page)session.getAttribute("page");
		if( pid <= 0 || aid == 0 ) {
			response.sendError(404);
		} else {
			if(article == null || page == null) {
				response.sendRedirect("article.jspx?_method=show&id="+aid+"&page="+pid);
				flag = true;
			} else if(article != null ){
				if(!id.equals(String.valueOf(article.getId()))) {
					response.sendRedirect("article.jspx?_method=show&id="+aid+"&page="+pid);
					flag = true;
				}
			}
			if(page != null){
				if( !p.equals(String.valueOf(page.getCurrentPage()))) {
					response.sendRedirect("article.jspx?_method=show&id="+aid+"&page="+pid);
					flag = true;
				}
			}
			if(!flag) {
				Render.rend("article", request, response);
			}
		}
	}
	
	/**
	 * 发帖跳转
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		String url = "index.jspx?_method=create&id="+id;
		if(user == null) {
			session.setAttribute("url", url);
			session.setAttribute("msg", "请登陆后再进行此操作");
			response.sendRedirect("index.jspx?_method=login");
		} else {
			Blocks blocks = new BlocksService().findById(id);
			request.setAttribute("blocks", blocks);
			Render.rend("newarticle", request, response);
		}
		
	}

	/**
	 * 登陆跳转
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String page = request.getParameter("page");
		if(id != null && page != null) {
			HttpSession session = request.getSession();
			String url = "article.jspx?_method=show&id="+id+"&page="+page;
			session.setAttribute("url", url);
		}
		Render.rend("login", request, response);
	}

	/**
	 * 注册跳转
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void register(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Render.rend("register", request, response);
	}

	/**
	 * 显示主页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void index(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		List<Blocks> list =	(List<Blocks>) session.getAttribute("blocksList");
		if(list != null) {
			response.sendRedirect("index.jspx?_method=showmain");
		} else {
			response.sendRedirect("blocks.jspx");
		}
	}
	
	/**
	 * 主页面跳转
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void showmain(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session =request.getSession();
		List<Blocks> blocksList = (List<Blocks>) session.getAttribute("blocksList");
		if(blocksList == null) {
			response.sendRedirect("blocks.jspx");
		} else {
			Render.rend("index", request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
